package com.koala.learn.excellearn;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.koala.learn.excellearn.entity.Student;
import com.koala.learn.excellearn.entity.Worker;
import org.junit.Test;

import java.io.File;
import java.time.LocalDateTime;
import java.util.ArrayList;

/**
 * @author yantingrui
 * @date 2021/12/27   17:29
 */
public class ExcelStart {

    public static void main(String[] args) {
        String fileName = "t12.xlsx";
        File file = new File(fileName);
        WriteSheet writeSheet = EasyExcel.writerSheet(fileName).build();
        ArrayList<Student> personArrayList = CollectionUtil.newArrayList();
        personArrayList.add(new Student("Koala", 8, null));
        personArrayList.add(new Student("Frank", 45, LocalDateTime.now().plusDays(15)));
        personArrayList.add(new Student("Frank", null));
        ExcelWriter writer = getWriter(file);
        writer.write(personArrayList, writeSheet);
        writer.finish();
        file.getFreeSpace();
    }


    /**
     *
     */
    @Test
    public void test38() {
        String fileName = "t12.xlsx";
        File file = new File(fileName);
        WriteSheet writeSheet = EasyExcel.writerSheet(fileName).build();
        ArrayList<Worker> personArrayList = CollectionUtil.newArrayList();
        personArrayList.add(new Worker("Koala", 8, null, "1"));
        personArrayList.add(new Worker("Frank", 45, LocalDateTime.now().plusDays(15), "2"));
        personArrayList.add(new Worker("Frank", null, null, "3"));
        ExcelWriter writer = getWriter(file);
        writer.write(personArrayList, writeSheet);
        writer.finish();
        file.getFreeSpace();
    }

    public static ExcelWriter getWriter(File file) {
        return EasyExcel.write(file, Worker.class).build();
    }
}
